package com.luguotao.lease.web.app.service.impl;

import com.luguotao.lease.common.context.LoginUserContext;
import com.luguotao.lease.model.entity.*;
import com.luguotao.lease.model.enums.ItemType;
import com.luguotao.lease.model.enums.LeaseStatus;
import com.luguotao.lease.web.app.mapper.*;
import com.luguotao.lease.web.app.service.ApartmentInfoService;
import com.luguotao.lease.web.app.service.BrowsingHistoryService;
import com.luguotao.lease.web.app.service.RoomInfoService;
import com.luguotao.lease.web.app.vo.apartment.ApartmentItemVo;
import com.luguotao.lease.web.app.vo.attr.AttrValueVo;
import com.luguotao.lease.web.app.vo.fee.FeeValueVo;
import com.luguotao.lease.web.app.vo.graph.GraphVo;
import com.luguotao.lease.web.app.vo.room.RoomDetailVo;
import com.luguotao.lease.web.app.vo.room.RoomItemVo;
import com.luguotao.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    ApartmentInfoService apartmentInfoService;
    @Autowired
    LeaseTermMapper leaseTermMapper;
    @Autowired
    PaymentTypeMapper paymentTypeMapper;
    @Autowired
    FeeValueMapper feeValueMapper;
    @Autowired
    AttrValueMapper attrValueMapper;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    LabelInfoMapper labelInfoMapper;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
    BrowsingHistoryService browsingHistoryService;


    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(roomItemVoPage,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        RoomInfo roomInfo = this.getById(id);

        //房间所属的公寓信息
        ApartmentItemVo apartmentItemVo=apartmentInfoService.selectApartmentById(roomInfo.getApartmentId());
        //房间的图片
        List<GraphInfo> graphVoList=graphInfoMapper.selectListByItemIdAndItemType(id,ItemType.ROOM);
        //房间的属性
        List<AttrValueVo> attrValueVoList=attrValueMapper.selectListByRoomId(id);
        //房间的配套
        List<FacilityInfo> facilityInfoList=facilityInfoMapper.selectListByRoomId(id);
        //房间的标签
        List<LabelInfo> labelInfoList=labelInfoMapper.selectListByRoomId(id);
        //房间的支付方式
        List<PaymentType> paymentTypeList=paymentTypeMapper.selectListByRoomId(id);
        //公寓的杂费
        List<FeeValueVo> feeValueVoList=feeValueMapper.selectListByApartmentId(roomInfo.getApartmentId());
        //房间的租期
        List<LeaseTerm> leaseTermList=leaseTermMapper.selectListByRoomId(id);
        //房间是否被删除
        Boolean isDelete=roomInfo.getIsDeleted()==1;
        //房间是否被入住
        Long count = leaseAgreementMapper.selectCount(new LambdaQueryWrapper<LeaseAgreement>().eq(LeaseAgreement::getRoomId, id).in(LeaseAgreement::getStatus, 2, 5));
        Boolean isCheckIn=count>0;

        roomDetailVo.setIsDelete(isDelete);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setIsCheckIn(isCheckIn);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);


        BeanUtils.copyProperties(roomInfo,roomDetailVo);

        //自动添加浏览记录
        //只能在此处获取登录用户的信息，因为后面异步实现线程就是异步的，获取不到用户信息
        browsingHistoryService.saveHistory(LoginUserContext.getUser().getUserId(), id);


        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(Page<RoomItemVo> roomItemVoPage, Long id) {
        //参数是公寓id
        return roomInfoMapper.pageItemByApartmentId(roomItemVoPage,id);
    }
}




